package com.zhuozhengsoft.pageoffice6springboot2back.controller;

import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice6springboot2back.po.DefineDataTagResult;
import com.zhuozhengsoft.pageoffice6springboot2back.util.AjaxResult;
import com.zhuozhengsoft.pageoffice6springboot2back.util.DataTagUtil;
import com.zhuozhengsoft.pageoffice6springboot2back.util.GetDirPathUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@RestController
@RequestMapping(value = "/DataTagEdit")
public class DataTagEditController {

    //获取doc目录的磁盘路径
    private final String dir = GetDirPathUtil.getDirPath() + "static/doc/";

    public DataTagEditController() throws IOException {
    }

    @RequestMapping(value = "/Word")
    public AjaxResult<DefineDataTagResult> openFile(HttpServletRequest request) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        poCtrl.webOpen("/doc/DataTagEdit/test.docx", OpenModeType.docNormalEdit, "张三");

        DataTagUtil.clearDataTagList();
        DataTagUtil.addDefineDataTag("{ 甲方 }");
        DataTagUtil.addDefineDataTag("{ 乙方 }");
        DataTagUtil.addDefineDataTag("{ 担保人 }");
        DataTagUtil.addDefineDataTag("【 合同日期 】");
        DataTagUtil.addDefineDataTag("【 合同编号 】");

        DefineDataTagResult defineDataTagResult=new DefineDataTagResult();
        defineDataTagResult.setDataTags(DataTagUtil.getDataTagList());
        defineDataTagResult.setPoHtml(poCtrl.getHtml());

        return AjaxResult.success(defineDataTagResult);
    }

    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        fs.saveToFile(dir + "DataTagEdit/" + fs.getFileName());
        fs.close();
    }
}
